package com.playfair.components;

import java.util.ArrayList;

import com.playfair.core.AppCore;

public class Digraph {
	private final ArrayList<Character[]> digraph;

	public Digraph(String text) {
		this.digraph = new ArrayList<Character[]>();
		generateDigraph(text);
//		printDigraph();
	}

	public void generateDigraph(String text) {
		text = text.replaceAll("\\s", "");
		text = text.toUpperCase();
		text = text.replace(AppCore.getInstance().getInterchangeChar1(),
				AppCore.getInstance().getInterchangeChar2());
		int textLength = text.length();
		for (int i = 0; i < textLength; i += 2) {
			Character[] tmp = new Character[2];
			if (i + 1 >= textLength) {
				tmp[0] = text.charAt(i);
				tmp[1] = AppCore.getInstance().getFinalChar();
			} else {
				if (text.charAt(i) == text.charAt(i + 1)) {
					tmp[0] = text.charAt(i);
					tmp[1] = AppCore.getInstance().getInterChar();
					i--;
				} else {
					tmp[0] = text.charAt(i);
					tmp[1] = text.charAt(i + 1);
				}
			}
			digraph.add(tmp);
		}
	}

	public void printDigraph() {
		for (Character[] pair : digraph)
			System.out.print(pair[0].toString() + pair[1].toString() + " ");
		System.out.println();
	}

	public ArrayList<Character[]> getDigraph() {
		return digraph;
	}

}
